package com.example.app.sharescreen;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.media.projection.MediaProjection;
import android.media.projection.MediaProjectionManager;
import android.os.IBinder;
import android.util.Log;

import androidx.fragment.app.FragmentActivity;

import org.webrtc.ScreenCapturerAndroid;
import org.webrtc.VideoCapturer;

public class ScreenCaptureManager {
    private static final String TAG = "ScreenCaptureManager";
    private static final int REQUEST_SCREEN_CAPTURE = 1001;
    
    private FragmentActivity activity;
    private ScreenCaptureService screenCaptureService;
    private boolean isBound = false;
    private ScreenCaptureCallback callback;
    
    public interface ScreenCaptureCallback {
        void onScreenCapturerReady(VideoCapturer capturer);
    }
    
    private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName className, IBinder service) {
            ScreenCaptureService.LocalBinder binder = (ScreenCaptureService.LocalBinder) service;
            screenCaptureService = binder.getService();
            isBound = true;
            
            if (callback != null) {
                Intent mediaProjectionIntent = screenCaptureService.getMediaProjectionIntent();
                if (mediaProjectionIntent == null) return;
                
                VideoCapturer videoCapturer = new ScreenCapturerAndroid(
                        mediaProjectionIntent,
                        new MediaProjection.Callback() {
                            @Override
                            public void onStop() {
                                Log.d(TAG, "Screen sharing stopped");
                                if (callback != null) {
                                    // Handle screen sharing stopped
                                }
                            }
                        });
                
                callback.onScreenCapturerReady(videoCapturer);
            }
        }
        
        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            isBound = false;
            screenCaptureService = null;
        }
    };
    
    public ScreenCaptureManager(FragmentActivity activity, ScreenCaptureCallback callback) {
        this.activity = activity;
        this.callback = callback;
    }
    
    public void requestScreenCapture() {
        MediaProjectionManager projectionManager =
                (MediaProjectionManager) activity.getSystemService(Context.MEDIA_PROJECTION_SERVICE);
        activity.startActivityForResult(
                projectionManager.createScreenCaptureIntent(),
                REQUEST_SCREEN_CAPTURE);
    }
    
    public void handleActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_SCREEN_CAPTURE && resultCode == FragmentActivity.RESULT_OK && data != null) {
            startScreenCaptureService(data);
        }
    }
    
    private void startScreenCaptureService(Intent data) {
        Intent serviceIntent = new Intent(activity, ScreenCaptureService.class);
        serviceIntent.putExtra("data", data);
        
        activity.startForegroundService(serviceIntent);
        activity.bindService(serviceIntent, connection, Context.BIND_AUTO_CREATE);
    }
    
    public void unbindService() {
        if (isBound) {
            activity.unbindService(connection);
            isBound = false;
        }
    }
    
    public boolean isBound() {
        return isBound;
    }
} 